package tjs.study.notes.eureka.clienta.user.service.feign;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;

/**
 * Created by Nicky.Tang on 2021/11/1 4:55 下午
 *
 * @since 02.12.10
 */
public class FeignConfig {
    private static final Logger logger = LoggerFactory.getLogger(FeignConfig.class);

    /**
     * 负载均衡规则
     *
     * @return
     * @desc
     */
    @Bean
    public IRule myRule() {
        return new RandomRule(); // 改为随机算法规则
        //return new ZoneAvoidanceRule(); // ZoneAvoidanceRule - 默认规则，复合判断server所在区域的性能和server的可用性进行服务的选择
    }

    /**
     * 开启请求日志
     *
     * @return
     */
    @Bean
    public feign.Logger.Level feignLoggerLevel() {
        return feign.Logger.Level.FULL;
    }

    /**
     * 请求拦截器
     *
     * @return
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                logger.info("请求 {} 接口...", requestTemplate.url());
                // 请求头加一些认证信息
                requestTemplate.header("JWT_TOKEN", "XXXXXX");
            }
        };
        return requestInterceptor;
    }
}
